Skip to content

printspool.device: the idle spooler waits instead of spinning - #2375

Open
terminills wants to merge 1 commit into
aros-development-team:masterfrom
terminills:printspool-idle-v1
Open

terminills wants to merge 1 commit into
aros-development-team:masterfrom
terminills:printspool-idle-v1

Conversation

@terminills

Copy link
Copy Markdown
Contributor

After its first job, the print spooler's process uses a whole CPU for as long as the system runs. A user saw it as a file monitor flooded with locks from the spooler, and as a Status file that a directory listing does not show.

What was wrong

With nothing to print, spooler_main() waits for its signal or for a two-second timer, and aborts the timer when the signal came first. AbortIO() replies the request, which sets the signal of the reply port; WaitIO() takes the reply off the port and leaves the signal set. The next Wait() returns at once, the timer that was just started is aborted in turn, and the loop never sleeps again. Each pass looks through the spool directory twice and writes ENV:PrintSpool/Status.

Measured in a guest (QEMU/KVM, two CPUs), counting the RAM disk's notifications for ENV:PrintSpool/Status:

pc-x86_64 pc-i386
writes of Status in the 20 s after one job was printed 447380 253946
looks at Status that did not find the file, of 200 34 53
host CPU used by the guest before any print 7 % 4 %
after the job, and from then on 104 % 100 %

The change

  • The timer port's signal is cleared after WaitIO(). With only this line added to master the spooler makes one pass every 2.0 s and the guest is back at its idle load, so this is the cause.
  • That still leaves a look at the queue and a write every two seconds for ever. PrintSpool/Status is now written when the state changes.
  • The spooler asks for notification on the spool directory (StartNotify(), a signal to the spooler). That is where PrintQueue holds, releases and cancels jobs, with control files and without a signal; it was the reason for the timer. The request follows the directory when SPOOL: is assigned or removed.
  • The timer remains for two cases. A spool directory whose file system refuses the notification, and then only while there are jobs in the queue: control files are about jobs, and a new job or a resumed queue comes with a signal. And a state that could not be written: SetVar() fails while another program has the file open to read it, and with no pass every two seconds nothing would write it again.
  • The device is 1.2.

Not changed

  • The loops inside a job: the retry every five seconds while a printer cannot be opened, and the one-second look while a job that is being sent is in a paused queue.
  • PrintQueue, which reads the queue on its own timer while its window is open.
  • A file system that accepts the notification request and then does not report files created in the directory would leave a release or a cancel unseen until the next job or resume. The RAM disk reports them; other file systems were not tried.

Tested

pc-x86_64 (SMP) and pc-i386, GCC 15.2.0. The guests are a full build of master with printspool.device replaced by the one under test; the test programs are built against that build's SDK. Jobs go to parallel.device unit 0, the spool directory is T:PrintSpool. One program writes a job through the device and does what PrintQueue does (CMD_STOP and CMD_START on a unit opened with PRDOF_SETTINGS, .hold and .cancel files); another counts notifications and date changes of T:PrintSpool, ENV:PrintSpool and ENV:PrintSpool/Status. x86_64:

master one line (signal cleared) this change this change, notification refused
writes of Status in 20 s, nothing to print 447380 10 0 0
the same with a job in a paused queue 386548 10 0 0
the same with a held job 283022 10 0 0
host CPU, nothing to print 104 % 8 % 7 % 7 %
host CPU, job in a paused queue 104 % 8 % 7 % 11 %
a cancelled job is gone after 0.0 s 1.0 s 0.0 s 0.9 s
a released job is printed after 0.0 s 0.1 s 0.0 s 0.0 s

"Notification refused" is a test build in which StartNotify() is taken as failed. On i386, master and this change were run: 253946 and 0 writes, 100 % and 4 % of a CPU, cancel and release seen after 0.0 s. In every run a single job, a job sent into a paused queue and cancelled, a job held and released, and three jobs in a row were printed or dropped as asked, and the state read idle afterwards.

The counting program looks at Status ten times a second, and in some runs of the one-line build its look made the spooler's write fail (5 and 0 writes counted in 20 s instead of 10). Builds that print a line before and after the idle Wait() give the count without that: the one-line build wakes every 2.0 s; this change does not wake in the 24 s after a job, and afterwards only for a job, a control file or a resume, never with a timer sent.

  • State that could not be written: a job waits for a device that cannot be opened, a program keeps Status open for 14 s, the job is cancelled meanwhile. Without the second try the state said waiting, cannot open nosuch.device unit 0 for good; with it, idle once the file was closed (x86_64 and i386).
  • Spool directory: with SPOOL: assigned to a directory in RAM: while the system runs, a job queued there was dropped when its .cancel file was written by hand; after SPOOL: was removed the same in T:PrintSpool. Both wake-ups came from the notification, no timer sent.

Not tested: SPOOL: on a disk file system, a network printer (ipp.device) as the target, PrintQueue's window itself, real hardware, and other architectures.

Once its first job was printed, the spooler process never slept again:
it looked through the spool directory and rewrote PrintSpool/Status
with "idle" some twenty thousand times a second in a QEMU guest, on one
CPU, until the system was shut down. A file monitor showed the
directory being locked without pause, and the Status file, made anew at
every pass, was not there for one look in five.

With nothing to print the spooler waits for its signal or for a
two-second timer, and aborts the timer when the signal came first.
AbortIO() replies the request, which sets the reply port's signal, and
WaitIO() takes the reply off the port without clearing it. The next
Wait() returned at once on that signal, the timer just started was
aborted in turn, and so on. The signal is now cleared after WaitIO().

That alone leaves a look at the queue and a write of the state every
two seconds for as long as the system runs. Both go:

- The state is written when it changes. A write fails while a program
  that shows the state has the file open; the idle spooler then tries
  again every two seconds until it went through.
- The spooler asks for notification on the spool directory, where the
  spooler UI holds, releases and cancels jobs with control files and
  without a signal. It follows the directory when SPOOL: is assigned
  or removed.
- Apart from that the timer is only used where the file system of the
  spool directory has no notification, and then only while there are
  jobs in the queue: control files are about jobs, and a new job comes
  with a signal.

The device is 1.2 with this change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant